
/**
** \file       case_clause.c
**
** \brief      this function parse the case_clause rule
**
** \author     Ben M'rad Hanna, Laterem Hamza, Boillot Ulysse
**             and De Olivera Guillaume
**
** \version    1.0
**
** \date       28 Novembre 2010
**
*/

#include "rules.h"

static s_caseclause     *ret(s_caseitem                 *citem)
{
  free(citem);
  citem = NULL;

  return (NULL);
}

static s_caseclause     *fct_repeat(s_caseclause         *cclause)
{
  s_caseitem            *tmp_citem = cclause->caseitem;

  while ((g_data->lexer != NULL)
         && (strcmp(g_data->lexer->token->id, ";;") == 0))
  {
    g_data->lexer = g_data->lexer->next;

    if (g_data->lexer == NULL)
      return (NULL);

    while (strcmp(g_data->lexer->token->id, "\n") == 0)
    {
      g_data->lexer = g_data->lexer->next;

      if (g_data->lexer == NULL)
        return (NULL);
    }

    tmp_citem->next = calloc(1, sizeof (s_caseitem));

    if (rule_case_item(tmp_citem->next) == NULL)
      return (ret(tmp_citem->next));

    tmp_citem = tmp_citem->next;
  }

  return (cclause);
}

s_caseclause            *rule_case_clause(s_caseclause          *cclause)
{
  if (g_data->lexer == NULL)
    return (NULL);

  cclause->caseitem = calloc(1, sizeof (s_caseitem));

  if (rule_case_item(cclause->caseitem) == NULL)
    return (ret(cclause->caseitem));

  if (g_data->lexer == NULL)
    return (ret(cclause->caseitem));

  if (strcmp(g_data->lexer->token->id, ";;") != 0)
  {
    if (fct_repeat(cclause) == NULL)
      return (ret(cclause->caseitem));
  }

  if ((g_data->lexer != NULL) && (strcmp(g_data->lexer->token->id, ";;") == 0))
    g_data->lexer = g_data->lexer->next;

  while ((g_data->lexer != NULL) && strcmp(g_data->lexer->token->id, "\n"))
    g_data->lexer = g_data->lexer->next;

  return (cclause);
}
